package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.dao.extend.SaleOrderItemExtendMapper;
import com.aiwei.tdjk.dto.request.SaleOrderItemPagerRequest;
import com.aiwei.tdjk.entity.SaleOrderItem;
import com.aiwei.tdjk.entity.SaleOrderItemExample;
import com.aiwei.tdjk.service.SaleOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SaleOrderItemServiceImpl implements SaleOrderItemService{

    @Autowired
    private SaleOrderItemExtendMapper saleOrderItemExtendMapper;


    /**
     * 分页查询
     * @param request
     * @return
     */
    @Override
    public Pager<SaleOrderItem> find(SaleOrderItemPagerRequest request) {
        request.webParamConvert();
        List<SaleOrderItem> list = saleOrderItemExtendMapper.find(request);
        int totalCount = saleOrderItemExtendMapper.getTotalCount(request);
        Pager<SaleOrderItem> pager = new Pager<>();
        pager.setPageNumber(request.getPageNumber());
        pager.setPageSize(request.getPageSize());
        pager.setList(list);
        pager.setTotalCount(totalCount);
        return pager;
    }
    /**
     * 新增
     *
     * @param saleOrderItem
     * @return
     */
    @Override
    public int add(SaleOrderItem saleOrderItem) {
        return saleOrderItemExtendMapper.insertSelective(saleOrderItem);
    }

    /**
     * 更新
     *
     * @param saleOrderItem
     */
    @Override
    public void update(SaleOrderItem saleOrderItem) {
        saleOrderItemExtendMapper.updateByPrimaryKeySelective(saleOrderItem);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        saleOrderItemExtendMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据订单号获取订单项目
     *
     * @param orderCode
     * @return
     */
    @Override
    public List<SaleOrderItem> getListByOrderCode(String orderCode) {
        SaleOrderItemExample example = new SaleOrderItemExample();
        example.or().andSaleordercodeEqualTo(orderCode);
        List<SaleOrderItem> saleOrderItems = saleOrderItemExtendMapper.selectByExample(example);
        return saleOrderItems;
    }

    /**
     * 根据订单项目号查询 产品的规格
     *
     * @param saleOrderItemCode
     * @return
     */
    @Override
    public String getStandardBysSaleOrderItemCode(String saleOrderItemCode) {

        SaleOrderItemExample example = new SaleOrderItemExample();
        example.or().andSaleorderitemcodeEqualTo(saleOrderItemCode);
        List<SaleOrderItem> saleOrderItems = saleOrderItemExtendMapper.selectByExample(example);
        return saleOrderItems.get(0).getStandard();
    }

}
